package gamesys.games.particlesystem.core {
	import gamesys.games.particlesystem.AbstractTest;

	import particlesystem.core.ParticleCanvas;
	import particlesystem.core.ParticleEffect;
	import particlesystem.core.ParticleSettings;
	import particlesystem.effects.DecayEffect;
	import particlesystem.effects.GravityEffect;
	import particlesystem.effects.GroundEffect;
	import particlesystem.effects.LifeEffect;
	import particlesystem.effects.VelocityEffect;
	import particlesystem.graphics.BitmapAnimationFactory;
	import particlesystem.utils.Range;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * @author robertthurston
	 */
	[SWF (frameRate="30", width="577", height="469", backgroundColor="0x222222")]

	public class ParticleEffectTest5 extends AbstractTest {

		private var particleEffect : ParticleEffect;
		private var particleCanvas : ParticleCanvas;
		private var particleSettings : ParticleSettings;
		private var emitter : Sprite;
		private var bitmapAnimationFactory : BitmapAnimationFactory;

		override protected function init() : void {
			createEmitter();
			drawGroundGuide();
			
			bitmapAnimationFactory = new BitmapAnimationFactory();
			
			particleCanvas = new ParticleCanvas(577, 469);
			
			particleSettings = new ParticleSettings();
			particleSettings.startBounds = new Rectangle(-25, 1, 50, 1);
			particleSettings.startDelayRange = new Range(0, 20);
			particleSettings.reSpawn = false;
			particleSettings.emitter = emitter; 
			particleSettings.bitmapAnimationParticles = bitmapAnimationFactory.getBitmapAnimationArray(["Coin0", "Coin1", "Coin2", "Coin3"]);
			
			particleEffect = new ParticleEffect(particleSettings, particleCanvas);
			particleEffect.addEffect(new LifeEffect(20, 30, 10));
			particleEffect.addEffect(new GravityEffect(1, 1));
			particleEffect.addEffect(new DecayEffect(.95, .99));
			particleEffect.addEffect(new GroundEffect(300, 350, .5));
			particleEffect.addEffect(new VelocityEffect(10, 20, -30, 30));
			
			addChild(particleCanvas);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function onClick(event : MouseEvent) : void {
			emitter.x = mouseX;
			emitter.y = mouseY;
			particleEffect.addParticles(10);
			particleSettings.startDelayRange = new Range(0, 100);
		}

		private function onEnterFrame(event : Event) : void {
			particleCanvas.clear();
			particleEffect.update();
		}

		private function drawGroundGuide() : void {
			var ground : Sprite = new Sprite();
			ground.graphics.beginFill(0xFF0000, .5);
			ground.graphics.drawRect(-(577 / 2), 0, 577, 50);
			ground.x = emitter.x;
			ground.y = emitter.y;
			addChild(ground);
		}

		private function createEmitter() : void {
			emitter = new Sprite();
			emitter.graphics.beginFill(0xFF0000);
			emitter.graphics.drawCircle(5, 5, 5);
			emitter.x = 275;
			emitter.y = 300;
			addChild(emitter);
		}
	}
}
